//      html.c
//      
//      Copyright 2011 DJ Shaji <djshaji@gmail.com>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include <gtk/gtk.h>
#include <string.h>

GList * x_get_urls1 ( gchar * text )
{
	gint i = 0, len = strlen ( text ) ;
	GList * list = NULL ;
	
	for ( i = 0 ; i < len - 4 ; i ++ )
	{
		if ( text [i] == 'h' )
		{
			if ( text [i+1] == 't' )
			{
				if ( text [i+2] == 't' )
				{
					if ( text [i+3] == 'p' )
					{
						gint y = i + 3 ;
						GString * s = g_string_new ( NULL ) ;
						
						for ( y = i ; y < len ; y ++ )
						{
							if ( y == '|' || y == '\'' || y == '\"' || y == '>' || y == '\n' )
								break ;
							
							g_string_append_c ( s, text [y] ) ;
						}
						
						g_message ( "%s", s -> str ) ;
						getchar () ;
						list = g_list_append ( list, g_string_free ( s, FALSE )) ;
					}
				}
			}
		}
		
	}
	
	return list ;
}

void x_print_urls ( GList * list )
{
	gint i = 0, len = g_list_length ( list ) ;
	
	for ( i = 0 ; i < len ; i ++ )
		g_message ( g_list_nth_data ( list, i )) ;
}

GList * x_get_urls ( gchar * text )
{
	GList * list = NULL ;
	gint i = 0 ;
	
	GString * s = g_string_new ( text ) ;

	for ( i = 0 ; i < s -> len ; i ++ )
	{
		gchar * head = strstr ( s -> str, "http://" ) ;
		if ( head == NULL )
			break ;
		
		if ( head [i] == '"' || head [i] == '\'' || head [i] == '>' || head [i] == '\n'|| head [i] == ')' )
		{
			gchar * url = g_strndup ( head, i ) ;
			g_string_erase ( s, 0, i ) ;
			
			list = g_list_append ( list, url ) ;
			i = 0 ;
		}
	}
	
	return list ;
}
